using Ausy.Silverlight.Survey.Infrastructure.Common;
using Microsoft.Practices.Unity;
using Ausy.Silverlight.Survey.Infrastructure.Context;
using System;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Ausy.Silverlight.Survey.Infrastructure.Events;

namespace Ausy.Silverlight.Survey.QuestionModule.ViewParts
{
    /// <summary>
    /// Cette classe représente le Vue Model pour le Result
    /// </summary>
    public class ResultViewModel : ViewModel<IResultView>, IResultViewModel
    {
        #region ResultTitle Property

        /// <summary>
        /// Property resultTitle
        /// </summary>
        private string resultTitle;

        /// <summary>
        /// Property resultTitle
        /// </summary>
        public string ResultTitle
        {
            get { return resultTitle; }
            set
            {
                if (resultTitle != value)
                {
                    resultTitle = value;
                    RaisePropertyChanged(() => this.ResultTitle);
                }
            }
        }

        #endregion

        #region ResultText Property

        /// <summary>
        /// Property resultText
        /// </summary>
        private string resultText;

        /// <summary>
        /// Property resultText
        /// </summary>
        public string ResultText
        {
            get { return resultText; }
            set
            {
                if (resultText != value)
                {
                    resultText = value;
                    RaisePropertyChanged(() => this.ResultText);
                }
            }
        }

        #endregion

        #region CloseCommand Command Property
        
        /// <summary>
        /// Commande de gestion de la fermeture 
        /// </summary>
        public ICommand CloseCommand { get; private set; } 

        #endregion

        #region Constructor

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        /// <param name="container">Container Unity pour instanciation des type d'objet</param>
        public ResultViewModel(IUnityContainer container)
            : base(container)
        {

        }

        #endregion

        #region Initialize Method

        /// <summary>
        /// Cette méthode permet l'initialisation du ViewModel avant que le DataContext soit affecté à la vue
        /// </summary>
        protected override void Initialize()
        {
            // Initialisation des données
            this.ResultTitle = "Votre Résultat";

            var context = this.container.Resolve<IApplicationContext>();
            this.ResultText = context.QuestionnaireResult.ToString("#,0.##") +"%";
            
            // Initialisation des commandes
            this.CloseCommand = new DelegateCommand<string>(this.OnClose);
        }

        #endregion

        /// <summary>
        /// Cette méthode permet de fermer l'application
        /// </summary>
        /// <param name="args"></param>
        public void OnClose(string args)
        {
            var context = this.container.Resolve<IApplicationContext>();
            context.QuestionnaireResult = 0;
            context.AccountIdentifier = Guid.Empty;
            context.AccountEmail = String.Empty;
            this.eventAggregator.GetEvent<NavigateEvent>().Publish(new NavigateEvent(NavigateUri.CreateAccountUri));
        }
    }
}
